package com.qiyi.player.core.history.parts
{
   import flash.utils.Timer;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.core.model.IMovie;
   import com.qiyi.player.base.rpc.RemoteObjectEvent;
   import com.qiyi.player.core.Config;
   import flash.events.TimerEvent;
   import com.qiyi.player.user.IUser;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import com.qiyi.player.core.history.events.HistoryEvent;
   import com.qiyi.player.core.player.events.PlayerEvent;
   import com.qiyi.player.core.player.def.PlayerUseTypeEnum;
   import com.qiyi.player.core.player.def.StatusEnum;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.base.logging.Log;
   
   public class LoginUserHistory extends BaseHistory
   {
      
      public function LoginUserHistory(param1:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.history.parts.LoginUserHistory");
         super(param1);
         _holder.addEventListener(PlayerEvent.Evt_StatusChanged,this.onVideoStatusChanged);
      }
      
      private var _timer:Timer;
      
      private var _firstUpdate:Boolean = false;
      
      private var _lastUploadTime:int = -2;
      
      private var _ready:Boolean = false;
      
      private var _downloadServer:DownloadServer;
      
      private var _uploadServer:UploadServer;
      
      private var _log:ILogger;
      
      override public function getReady() : Boolean {
         return this._ready;
      }
      
      override public function installMovie(param1:IMovie) : void {
         if(_movie == param1)
         {
            return;
         }
         super.installMovie(param1);
         this._downloadServer = new DownloadServer(param1.tvid);
         this._downloadServer.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onDownloadServerStatusChanged);
         this._downloadServer.initialize();
         this._lastUploadTime = -2;
         this._firstUpdate = false;
         this._ready = false;
         _history = null;
         if(this._timer == null)
         {
            this._timer = new Timer(Config.HISTORY_LOGIN_USER_UPLOAD_RATE);
            this._timer.addEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.start();
         }
         else if(!this._timer.running)
         {
            this._timer.reset();
            this._timer.start();
         }
         
      }
      
      override public function uninstallMovie(param1:IMovie) : void {
         super.uninstallMovie(param1);
         if(this._downloadServer)
         {
            this._downloadServer.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onDownloadServerStatusChanged);
            this._downloadServer.destroy();
            this._downloadServer = null;
         }
         if(this._timer)
         {
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.stop();
            this._timer = null;
         }
      }
      
      private function onTimer(param1:TimerEvent) : void {
         this.flush();
      }
      
      override public function load(param1:IUser) : void {
         _user = param1;
         this._firstUpdate = false;
      }
      
      override public function update(param1:int) : void {
         if(_movie == null)
         {
            return;
         }
         super.update(param1);
         if(_history == null)
         {
            _history = {};
         }
         _history.tvId = _movie.tvid;
         _history.terminalId = "11";
         _history.videoPlayTime = int(param1 / 1000);
         if(!this._firstUpdate)
         {
            if(param1 != 0)
            {
               this.flush();
               this._firstUpdate = true;
            }
         }
      }
      
      private function onDownloadServerStatusChanged(param1:RemoteObjectEvent) : void {
         var _loc2_:DownloadServer = param1.target as DownloadServer;
         if(_loc2_.status == RemoteObjectStatusEnum.Processing)
         {
            return;
         }
         this._ready = true;
         var _loc3_:String = _loc2_.tvid;
         _history = _loc2_.getData();
         if((_history) && !_history.hasOwnProperty("tvId"))
         {
            _history = null;
         }
         if(_history == null)
         {
            this.update(-1000);
         }
         _loc2_.destroy();
         _loc2_.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onDownloadServerStatusChanged);
         this._downloadServer = null;
         dispatchEvent(new HistoryEvent(HistoryEvent.Evt_Ready,_loc3_));
      }
      
      override public function flush() : void {
         if(_history == null)
         {
            this._log.warn("LoginUserHistory failed to upload history, for the histories is empty!");
         }
         else if(this._lastUploadTime != _history.videoPlayTime)
         {
            if(this._uploadServer)
            {
               this._uploadServer.destroy();
            }
            this._uploadServer = new UploadServer(_history);
            this._uploadServer.initialize();
            this._lastUploadTime = int(_history.videoPlayTime);
         }
         
      }
      
      private function onVideoStatusChanged(param1:PlayerEvent) : void {
         if((_holder.runtimeData.recordHistory) && _holder.runtimeData.playerUseType == PlayerUseTypeEnum.MAIN)
         {
            switch(_holder.status)
            {
               case StatusEnum.PAUSED:
               case StatusEnum.FAILED:
                  if(this._firstUpdate)
                  {
                     this.flush();
                  }
                  break;
               case StatusEnum.PLAYING:
                  if(!this._firstUpdate)
                  {
                     if(_holder.currentTime < 15000)
                     {
                        this.update(-1000);
                     }
                  }
                  break;
            }
         }
      }
      
      override public function destroy() : void {
         super.destroy();
         if(this._timer)
         {
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.stop();
            this._timer = null;
         }
         this._firstUpdate = false;
         if(this._downloadServer)
         {
            this._downloadServer.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onDownloadServerStatusChanged);
            this._downloadServer = null;
         }
         if(this._uploadServer)
         {
            this._uploadServer.destroy();
            this._uploadServer = null;
         }
         if(_holder)
         {
            _holder.removeEventListener(PlayerEvent.Evt_StatusChanged,this.onVideoStatusChanged);
         }
      }
   }
}
import com.qiyi.player.base.rpc.impl.BaseRemoteObject;
import com.qiyi.player.base.logging.ILogger;
import flash.net.URLRequest;
import com.qiyi.player.core.Config;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.Event;
import flash.utils.clearTimeout;
import com.adobe.serialization.json.JSON;
import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
import com.qiyi.player.base.logging.Log;

class UploadServer extends BaseRemoteObject
{
   
   function UploadServer(param1:Object) {
      this._log = Log.getLogger("com.qiyi.player.core.history");
      super(0,"LoginUserHistoryUploadServer");
      this._updateData = param1;
      _retryMaxCount = 5;
   }
   
   private var _updateData:Object;
   
   private var _log:ILogger;
   
   override protected function getRequest() : URLRequest {
      var _loc1_:URLRequest = new URLRequest(Config.HISTORY_LOGIN_USER_UPLOAD_URL);
      _loc1_.method = URLRequestMethod.POST;
      var _loc2_:URLVariables = new URLVariables();
      _loc2_.tvId = this._updateData.tvId;
      _loc2_.terminalId = this._updateData.terminalId;
      _loc2_.videoPlayTime = this._updateData.videoPlayTime;
      this._log.debug("upload time:" + _loc2_.videoPlayTime);
      _loc1_.data = _loc2_;
      return _loc1_;
   }
   
   override protected function onComplete(param1:Event) : void {
      var obj:Object = null;
      var event:Event = param1;
      clearTimeout(_waitingResponse);
      _waitingResponse = 0;
      try
      {
         obj = com.adobe.serialization.json.JSON.decode(_loader.data);
         if(obj.code != "A00000")
         {
            this._log.info("failed to upload history! errorcode: " + obj.code);
         }
         else
         {
            this._log.info("success to upload history!");
         }
         super.onComplete(event);
      }
      catch(e:Error)
      {
         _log.warn(_name + ": failed to parse data--> " + _loader.data.substr(0,100));
         setStatus(RemoteObjectStatusEnum.DataError);
      }
   }
}
import com.qiyi.player.base.rpc.impl.BaseRemoteObject;
import com.qiyi.player.base.logging.ILogger;
import com.qiyi.player.base.pub.ProcessesTimeRecord;
import flash.utils.getTimer;
import flash.net.URLRequest;
import com.qiyi.player.core.Config;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.Event;
import flash.utils.clearTimeout;
import com.adobe.serialization.json.JSON;
import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
import com.qiyi.player.base.logging.Log;

class DownloadServer extends BaseRemoteObject
{
   
   function DownloadServer(param1:String) {
      this._log = Log.getLogger("com.qiyi.player.core.history");
      super(0,"LoginUserHistoryDownloadServer");
      this._tvid = param1;
      _timeout = 2000;
   }
   
   private var _tvid:String;
   
   private var _log:ILogger;
   
   public function get tvid() : String {
      return this._tvid;
   }
   
   override public function initialize() : void {
      ProcessesTimeRecord.STime_history = getTimer();
      super.initialize();
   }
   
   override protected function getRequest() : URLRequest {
      var _loc1_:URLRequest = new URLRequest(Config.HISTORY_LOGIN_USER_READ_RECORD);
      _loc1_.method = URLRequestMethod.POST;
      var _loc2_:URLVariables = new URLVariables();
      _loc2_.tvId = this._tvid;
      _loc2_.limit = 15;
      _loc1_.data = _loc2_;
      return _loc1_;
   }
   
   override protected function onComplete(param1:Event) : void {
      var obj:Object = null;
      var historyItem:Object = null;
      var historyTimeData:Object = null;
      var event:Event = param1;
      clearTimeout(_waitingResponse);
      _waitingResponse = 0;
      ProcessesTimeRecord.usedTime_history = getTimer() - ProcessesTimeRecord.STime_history;
      try
      {
         obj = com.adobe.serialization.json.JSON.decode(_loader.data);
         if(obj.code != "A00000")
         {
            this._log.info("LoginUserHistoryDownloadServer failed to load history! errorcode: " + obj.code);
            super.onComplete(event);
         }
         else
         {
            historyItem = obj.data;
            historyTimeData = {};
            historyTimeData.tvId = historyItem.tvId;
            historyTimeData.videoPlayTime = int(historyItem.videoPlayTime);
            historyTimeData.addtime = historyItem.addtime;
            this._data = historyTimeData;
            this._log.debug("download histroy time:" + historyItem.videoPlayTime);
            super.onComplete(event);
         }
      }
      catch(e:Error)
      {
         _log.warn(_name + ": failed to parse data--> " + _loader.data.substr(0,100));
         setStatus(RemoteObjectStatusEnum.DataError);
      }
   }
}
